package com.gio.tools.modules.cdp.controller;

import com.gio.tools.common.config.ProjectConfig;
import com.gio.tools.common.utils.DateUtils;
import com.gio.tools.common.utils.GrowingIoUtils;
import com.gio.tools.common.utils.HiveUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务表Controller
 *
 * @author tm
 * @version 2020/12/15 17:20
 */
@Controller
@RequestMapping(value = "/api/v1")
public class ApiController {

    @Autowired
    @Qualifier("hiveDruidDataSource")
    DataSource druidDataSource;
    @Autowired
    private ProjectConfig projectConfig;

    @RequestMapping(value = "/tem")
    @ResponseBody
    public void tem() throws SQLException, InterruptedException {
//        String sql = "select * from gio.order_11 a left join gio.tmp_ordertype b on b.order_type = a.order_type where a.order_status = '2'";
        String sql = "select * from gio.order_11 a left join gio.tmp_ordertype b on b.order_type = a.order_type where a.order_status = '2' limit 1";
        HiveUtils hiveUtils = new HiveUtils(druidDataSource);
        ResultSet res = hiveUtils.runSql(sql);
        // 发送user
        int number = 0;
        System.out.println("--------------------正在发送用户时间，操作时间为：" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "-------------------------");
        while (res.next()) {
            Thread.sleep(projectConfig.getSendInterval());
            // {"event":"crmOrder","userId":"14796585278","timestamp":1604160008000,"attrs":{"orderNumber_var":"22020110112000121966230631978110","ordertype_var":"6","payType_var":"4","goodsId_var":"55737","skuId_var":"96828","skuName_var":"","goodsType_var":"GT0005","goodsName_var":"5元2GB流量日包","orderTypeName_var":"流量"}}
            Map<String, Object> map = new HashMap<>(16);
            map.put("orderNumber_var", res.getString("order_id"));
            map.put("ordertype_var", res.getString("order_type"));
            map.put("payType_var", res.getString("pay_way"));
            map.put("goodsId_var", res.getString("goods_id"));
            map.put("skuId_var", res.getString("sku_id"));
            map.put("goodsType_var", res.getString("goods_type"));
            map.put("goodsName_var", res.getString("goods_name"));
            map.put("skuName_var", "");
            map.put("orderTypeName_var", res.getString("ORDER_TYPENAME"));

            GrowingIoUtils.sendCdpEvent("crmOrder", res.getString("user_name"), DateUtils.parseDate(res.getString("finish_time")).getTime(), map);
            number ++;
        }
        System.out.println("--------------------用户信息发送完毕" +  "，总数为：" + number + "-------------------------");
        hiveUtils.destruction();
    }

    /**
     * 发送事件
     * @param sql
     * @param eventKey
     * @param userIdField 用户id  字段
     * @param eventTimeField   eventTime 字段
     * @param varToFields 格式为    事件参数key1-字段名1,事件参数key2-字段名2
     * @param isLog 是否打印日志    1：打印每条数据的userId
     * @throws SQLException
     * @throws InterruptedException
     */
    @RequestMapping(value = "/sendCdpEvent")
    @ResponseBody
    public void sendCdpEvent(String sql, String eventKey, String userIdField, String eventTimeField, String[] varToFields, String isLog) throws SQLException, InterruptedException {
        GrowingIoUtils.queryAndSendCdpEvent(druidDataSource, projectConfig, sql, varToFields, eventKey, userIdField, eventTimeField, isLog);
    }

    /**
     * 发送事件
     * @param sql
     * @param userIdField 用户id  字段
     * @param varToFields 格式为    事件参数key1-字段名1,事件参数key2-字段名2
     * @param isLog 是否打印日志    1：打印每条数据的userId
     * @throws SQLException
     * @throws InterruptedException
     */
    @RequestMapping(value = "/sendCdpUser")
    @ResponseBody
    public void sendCdpUser(String sql, String userIdField, String[] varToFields, String isLog) throws SQLException, InterruptedException {
        GrowingIoUtils.queryAndSendCdpUser(druidDataSource, projectConfig, sql, varToFields, userIdField, isLog);
    }

}
